<?php
/**
 * Aero - Xistins Technologies PHP Framework
 * 
 * LICENSE
 * This source file is subject to the New BSD license that is bundled 
 * with this package in the LICENSE.txt. It is also available through 
 * the world-wide-web at this URL: http://dev.xistins.com/license/production
 * If you did not receive a copy of this license and are unable to obtain
 * it through the world-wide-web, please send an email to 
 * license@xistins.com so we can send you a copy immediately.
 * 
 * @category   Aero
 * @package    Aero_Controller
 * @subpackage Page
 * @copyright  http://dev.xistins.com/license/production     New BSD
 * @version    $Id: Page.php 4 2009-11-14 18:35:45Z csouth@xistins.com $
 */

/** @see Aero_Controller_Application **/
require_once 'Aero/Controller/Application.php';

/**
 * Page controller
 * 
 * @category   Aero
 * @package    Aero_Controller
 * @subpackage Page
 * @copyright  http://dev.xistins.com/license/production     New BSD
 * @version    $Id: Page.php 4 2009-11-14 18:35:45Z csouth@xistins.com $
 */
class Aero_Controller_Page
{
    /**
     * Application Controller
     * @var Aero_Controller_Application
     */
    protected $_app;

    /**
     * If action should load view script
     * @var bool
     */
    protected $_loadViewScript = TRUE;

    /**
     * Request Params
     * @var array
     */
    protected $_params = array();

    /**
     * Application View Class
     * @var Aero_View
     */
    public $view;

    /**
     * Page Constructor
     * 
     * @param  string $requestedAction
     * @param  array  $params
     * @return void
     */
    public function __construct()
    {
        $this->_app = Aero_Controller_Application::getInstance();
        $this->view = $this->_app->getView();
        $this->_params = $this->_app->getParams();
        $this->_init();

        call_user_func(array($this, strtolower($this->_app->getRequestedAction()).'Action'));

        if($this->renderView()) {
            $this->view->content = $this->view->render($this->_getScript());
            echo $this->view->renderLayout();
        }
    }

    /**
     * Controller specific initialization.
     * 
     * This is here to be extended by the developer.
     * This is called by {@link __construct()} at the end of construct
     * 
     * @return void
     */
    protected function _init()
    {}

    /**
     * Get requested actions script with path
     * 
     * @return string
     */
    protected function _getScript()
    {
        $path  = $this->_app->getModuleDirectory().'/';
        $path .= $this->_app->getRequestedModule().'/views/';
        $path .= $this->_app->getRequestedController().'/';
        return $path.strtolower($this->_app->getRequestedAction()).'.phtml';
    }

    /**
     * Turn View renderer on or off.
     * 
     * @param  bool $render
     * @return Aero_Controller_Page
     */
    public function renderView($render = NULL)
    {
        if(NULL !== $render) {
            $this->_loadViewScript = $render;
        }

        return $this->_loadViewScript;
    }
}